import AMap from "AMap";
export default class MapTool {
  constructor(position) {
    this.position = position;
    this.clusts = [];
    this.markers = [];
    let map;
    map = new AMap.Map("container", {
      center: position,
      resizeEnable: true,
      zoom: 12
    });
    AMap.plugin(["AMap.ToolBar"], function() {
      map.addControl(
        new AMap.ToolBar({
          liteStyle: true,
          position: "LT"
        })
      );
    });
    this.aMap = map;
  }

  addClusterMarker(markData, callback) {
    let markers = [];
    for (let item of markData) {
      let icons = this.requireIcons(item.martIcon);
      let images = new AMap.Icon({
        image: icons,
        size: new AMap.Size(40, 40),
        imageSize: new AMap.Size(40, 40)
      });
      // let contents =
      //   `<div class="map_pic"><img class="map_pic_icon" src=` + icons + ` />`;
      let marker = new AMap.Marker({
        position: item.position,
        // content: contents,
        extData: item,
        icon: images
      });
      marker.on("click", function() {
        var items = [];
        items.push(marker);
        callback(items);
      });
      markers.push(marker);
    }
    return markers;
  }

  //增加点的聚合
  addCluster(markerDataList, callback) {
    _this=this;
    let _renderClusterMarker = function(context) {
      let icons = _this.requireIcons(markerDataList[0].martIcon);
      let contents =
        `<div class="map_pic"><img class="map_pic_icon" src=` +
        icons +
        ` /><span class="map_pic_text">` +
        context.count +
        `</span></div>`;
      context.marker.setContent(contents);
      // context.marker.on("click", function(mark) {
      //   console.log(mark.markers);
      // });
    };
    let markerList = this.addClusterMarker(markerDataList, callback);
    this.markers.push(markerList);
    let _this = this;
    AMap.plugin(["AMap.MarkerClusterer"], function() {
      let cluster = new AMap.MarkerClusterer(_this.aMap, markerList, {
        gridSize: 30,
        renderClusterMarker: _renderClusterMarker,
        zoomOnClick: false,
        maxZoom: 19
      });
      _this.clusts.push(cluster);
      //为覆盖物增加点击事件，并且获取图标id
      cluster.on("click", clust => {
        for (let singleMarker of clust.markers) {
          console.log(singleMarker.getExtData());
        }
        callback(markerList);
      });
    });
  }

  //根据不同的图片获取本地切图
  requireIcons(iconNum) {
    switch (iconNum) {
      case 0:
        return require("@/assets/images/icon_building_alarm.png");
      case 1:
        return require("@/assets/images/icon_gas_alarm.png");
      case 2:
        return require("@/assets/images/icon_methane_alarm.png");
      case 3:
        return require("@/assets/images/icon_gas_alarm_second.png");
      case 4:
        return require("@/assets/images/icon_gas_alarm_third.png");
    }
  }

  clearMapmakers() {
    this.clusts.forEach((clurt, index) => {
      this.markers[index].forEach(marker => {
        clurt.removeMarker(marker);
      });
    });
    this.clusts = [];
    this.markers = [];
  }

  //设置中心点
  setCenter(center) {
    this.aMap.setCenter(center);
  }
  //放大的maker恢复默认尺寸
  resumemarker(e){
    console.log(e)
    if(Object.keys(e).length != 0){
      var path=e.path;
      var marker=e.marker;
      let newIcon = new AMap.Icon({
        image: path,
        size: new AMap.Size(40, 40),
        imageSize: new AMap.Size(40, 40)
      });
      marker.setIcon(newIcon);
    }
  }

}
